#ifndef _SBUS_HT10A_H
#define _SBUS_HT10A_H

#include "stdint.h"
#include "bsp_uni_remote.h"

#pragma pack(push, 1)
typedef struct
{
    uint8_t header;    // 帧头 0x0F
    uint8_t data[22];  // 通道数据区
    uint8_t flags;     // 标志位
    uint8_t footer;    // 帧尾 0x00
} SBUS_RawData;
#pragma pack(pop)

#define SBUS_RECEIVE_BUFFER 25

// SBUS通道值范围定义
#define SBUS_CH_MIN 192
#define SBUS_CH_MID 992
#define SBUS_CH_MAX 1792

// 标志位定义
#define SBUS_FLAG_CH17 0x01
#define SBUS_FLAG_CH18 0x02
#define SBUS_FLAG_LOST 0x04
#define SBUS_FLAG_FAILSAFE 0x08

// 连接超时定义 (ms)
#define SBUS_TIMEOUT_MS 100

// 解析状态
typedef struct
{
    SBUS_RawData raw_data;
    uint8_t connected;
    uint32_t last_update;
    // 所有16个通道的原始值 (11位，0-2047)
    uint16_t channels_raw[16];
    /**
         通道值对应遥感映射关系
         channels_raw[0] 通道1: 右摇杆X 192~992~1792
         channels_raw[1] 通道2: 右摇杆Y 192~992~1792
         channels_raw[2] 通道3: 左摇杆Y 192~992~1792
         channels_raw[3] 通道4: 左摇杆X 192~992~1792
         channels_raw[4] 通道5: SWA-5  192/992/1792   三档自弹
         channels_raw[5] 通道6: SWB-6  192/1792       两档
         channels_raw[6] 通道7: SWC-7  192/1792       两档
         channels_raw[7] 通道8: SWD-8  192/992/1792   三档
         channels_raw[8] 通道9: VRA    192~1792       旋钮
         channels_raw[9] 通道10 VRB    192~1792       旋钮
         channels_raw[x] 通道11~16                    保留
    **/
    // 数字通道
    uint8_t ch17;
    uint8_t ch18;
    // 状态标志
    uint8_t frame_lost;
    uint8_t failsafe;
} SBUS_HandleTypeDef;

// 函数声明
void SBUS_Init(SBUS_HandleTypeDef* hsbus);      //初始化
uint8_t SBUS_Update(SBUS_HandleTypeDef* hsbus, uint8_t* data, uint32_t len);//写入数据
void SBUS_ParseChannels(SBUS_HandleTypeDef *hsbus);
int16_t SBUS_To_RBUS_Range(uint16_t sbus_value);
uint8_t SBUS_Map_RBUS_Switch(uint16_t channel_value);
uint8_t SBUS_CheckTimeout(SBUS_HandleTypeDef* hsbus);
void SBUS_PrintChannels(SBUS_HandleTypeDef* hsbus);
void SBUS_To_UniRemote(UniRemote_HandleTypeDef* hur, SBUS_HandleTypeDef* hsbus);
#endif